package main

import "strings"

func main() {

	//strsArray := []string{"a", "b", "c"}

	//测试用例
	//nums := 1234
	str := "MCMXCIV"

	//打印测试用例返回结果
	resp := romanToInt(str)
	println(resp)

}

func romanToInt(s string) int {
	//根据提示作出校验：
	if len(s) <= 1 && len(s) >= 15 {
		return -99999
	}
	//将string转换成字符串数组
	strArr := strings.Split(s, "")

	singleMap := make(map[string]int)
	singleMap["I"] = 1
	singleMap["V"] = 5
	singleMap["X"] = 10
	singleMap["L"] = 50
	singleMap["C"] = 100
	singleMap["D"] = 500
	singleMap["M"] = 1000

	doubleMap := make(map[string]int)
	doubleMap["IV"] = 4
	doubleMap["IX"] = 9
	doubleMap["XL"] = 40
	doubleMap["XC"] = 90
	doubleMap["CD"] = 400
	doubleMap["CM"] = 900

	ans := 0
	for i := 0; i < len(strArr); i++ {

		if i == len(strArr)-1 { //如果只有一个元素，或者是遍历到还剩最后一个元素
			ans = ans + singleMap[strArr[i]]
			break
		}

		if singleMap[strArr[i]] < singleMap[strArr[i+1]] {
			ans = ans + doubleMap[strArr[i]+strArr[i+1]]
			i++
		} else {
			ans = ans + singleMap[strArr[i]]
		}
	}

	return ans
}
